<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>5-08 调节RGB通道</title>
</head>
<body>
  <canvas id="c" width="1000" height="600" style="border: 1px solid #ccc;"></canvas>

  <script>
    const context = document.getElementById('c')
    const ctx = context.getContext('2d')

    const img = new Image()
    img.src = '../images/desert.jpg'

    img.onload = () => {
      ctx.drawImage(img, 10, 10)

      const imgData = ctx.getImageData(10, 10, 300, 200)

      let arg = 80 // 调节参数

      // r 红通道
      const r = ctx.createImageData(imgData)
      r.data.set(imgData.data)
      const rData = r.data

      for (var i = 0; i < rData.length; i += 4) {
        rData[i + 0] += arg
      }

      ctx.putImageData(r, 10, 310)

      // g 绿通道
      const g = ctx.createImageData(imgData)
      g.data.set(imgData.data)
      const gData = g.data

      for (var i = 0; i < gData.length; i += 4) {
        gData[i + 1] += arg
      }

      ctx.putImageData(g, 340, 310)

      // b 蓝通道
      const b = ctx.createImageData(imgData)
      b.data.set(imgData.data)
      const bData = b.data

      for (var i = 0; i < bData.length; i += 4) {
        bData[i + 2] += arg
      }

      ctx.putImageData(b, 670, 310)
    }
  </script>
</body>
</html>